import datarover from "./datarover.mjs";
import { nearest_data } from "./nearest.mjs";

export default function (container, template) {
  const reg = /\[\[:?[a-zA-Z0-9\-\./_]+\]\]/gi;
  if (typeof template === "function") {
    template = template({ container, data: nearest_data(container) });
  }
  return template.replace(reg, (m) => {
    let path = m.replace("[[", "").replace("]]", "");
    let d = datarover(container, path);
    return d === undefined || d === null ? "" : d;
  });
}
